package com.yyl.demo.render

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import com.yyl.demo.databinding.ActivityRgbrenderBinding
import com.yyl.render.RenderConfig.i
import com.yyl.render.render.RGBARender
import java.io.IOException
import java.nio.ByteBuffer

class RGBRender : AppCompatActivity() {
    private lateinit var binding: ActivityRgbrenderBinding


    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRgbrenderBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.seekBar1.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(
                    it.toFloat(),
                    0f,
                    1.0f,
                    1.0f,
                    0,
                    0
                )
            }
        )
        binding.seekBar2.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(
                    0f,
                    0f,
                    it.toFloat() + 1.0f,
                    1.0f,
                    0,
                    0
                )
            }
        )
        binding.seekBar3.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(0.0f, 0f, 0.2f, 0.5f, it, 0)
            }
        )
        binding.frame.setOnClickListener {
            loadYUV()
//            render.updateRGBA(640,480,bytes)
        }
        binding.surfaceView.attachRender(render)
//        loadRGBAImage("front.jpg")
        i("RGBRender","onCreate")
    }
    val render = RGBARender()

    fun loadYUV(){
        val byteArray = assets.open("yuv/yyl640_480.yuv").readBytes()
        render.updateYUV(
           640,
           480,
            byteArray
        )
    }

    fun loadRGBAImage() {
        val inputStream = assets.open("front.jpg")
        try {
            val bitmap = BitmapFactory.decodeStream(inputStream)
            if (bitmap != null) {
                val buf = ByteBuffer.allocate(bitmap.byteCount)
                bitmap.copyPixelsToBuffer(buf)
                val byteArray = buf.array()
                render.updateRGBA(
                    bitmap.width,
                    bitmap.height,
                    byteArray
                )
            }
        } finally {
            try {
                inputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    fun getSeek(callback: (progress: Int) -> Unit) =
        object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                callback.invoke(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        }
}